package com.ruoyi.web.controller.common;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gd.mail.domain.bo.ZMailBo;
import com.ruoyi.gd.mail.domain.vo.ZMailVo;
import com.ruoyi.gd.mail.service.IZMailService;
import com.ruoyi.gd.wgd.domain.bo.ZWgdBo;
import com.ruoyi.gd.wgd.domain.vo.ZWgdVo;
import com.ruoyi.gd.wgd.service.IZWgdService;
import com.ruoyi.gd.ywd.domain.ZYwd;
import com.ruoyi.gd.ywd.domain.bo.ZYwdBo;
import com.ruoyi.gd.ywd.domain.vo.ZYwdVo;
import com.ruoyi.gd.ywd.service.IZYwdService;
import com.ruoyi.gd.ywdwgd.domain.ZYwdwgd;
import com.ruoyi.gd.ywdwgd.domain.bo.ZYwdwgdBo;
import com.ruoyi.gd.ywdwgd.domain.vo.ZYwdwgdVo;
import com.ruoyi.gd.ywdwgd.service.IZYwdwgdService;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


/**
 * 客户信息
 *
 * @author ruoyi
 * @date 2024-03-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/home")
public class HomeController extends BaseController {


    @Autowired
    private IZYwdService ywdService;

    @Autowired
    private IZWgdService wgdService;

    @Autowired
    private IZMailService mailService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IZYwdwgdService ywdwgdService;


    /**
     * 获取首页数据
     */
    @GetMapping("/list")
    public JSONObject list(@RequestParam int pageNum, @RequestParam int pageSize){
        // 封装分页参数
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(pageNum);
        pageQuery.setPageSize(pageSize);
        pageQuery.setOrderByColumn("createtime desc");
        // 创建返回object
        JSONObject jsonObject = new JSONObject();

        // 查询下场安排数据
//        ZYwdBo zYwdBo = new ZYwdBo();
//        zYwdBo.setSfyxc("1");  // 查询待安排
//        zYwdBo.setSjzt("1");  // 是否已下场
//        TableDataInfo<ZYwdVo> xcapList = ywdService.queryPageList(zYwdBo, pageQuery, "xcap");
//        jsonObject.put("xcapList", xcapList.getRows());

        // 查询完工单
        ZWgdBo zWgdBo = new ZWgdBo();
        zWgdBo.setSfhk("2,3");
        zWgdBo.setSfykfp("N");
        TableDataInfo<ZWgdVo> wgdList = wgdService.queryPageHomeList(zWgdBo, pageQuery);
        jsonObject.put("wgdList", wgdList.getRows());

        return jsonObject;
    }


    /**
     * 获取数控来那个
     */
    @GetMapping("/getDbNum")
    public JSONObject list(){
        // 1. 返回的数据
        JSONObject resultData = new JSONObject();
        // 2.创建分类
        JSONArray wgdList = new JSONArray();
        JSONArray qtList = new JSONArray();
        // 待录入完工单今天
        JSONObject dlrwgdjt = new JSONObject();
//        JSONObject dlrwgdjtResult = getDzwgdData("dlrwgdjt", "dlr");
        JSONObject dlrwgdjtResult = getWgdQuery("dlrwgdjt", "dlr");
        JSONArray one = new JSONArray();
        dlrwgdjt.put("title", "待录完工单(今天)");
        dlrwgdjt.put("type", "dlrwgdjt");
        dlrwgdjt.put("number", dlrwgdjtResult.get("number"));
        dlrwgdjt.put("result", dlrwgdjtResult);
        dlrwgdjt.put("path", "/gd/ywdwgd");
        one.put(dlrwgdjt);
        // 待完善完工单今天
        JSONObject dwswgdjt = new JSONObject();
//        JSONObject dwswgdjtResult = getDzwgdData("dwswgdjt", "dws");
        JSONObject dwswgdjtResult = getWgdQuery("dwswgdjt", "dws");
        dwswgdjt.put("title", "待完善完工单(今天)");
        dwswgdjt.put("type", "dwswgdjt");
        dwswgdjt.put("number", dwswgdjtResult.get("number"));
        dwswgdjt.put("result", dwswgdjtResult);
        dwswgdjt.put("path", "/gd/ywdwgd");
        one.put(dwswgdjt);

        // 待录入完工单定义
        JSONObject dlrwgddy = new JSONObject();
//        JSONObject dlrwgddyResult = getDzwgdData("dlrwgdzdy", "dlr");
        JSONObject dlrwgddyResult = getWgdQuery("dlrwgdzdy", "dlr");
        // 解析时间
        Date endXcsj = dlrwgddyResult.getDate("endTime");
        JSONArray two = new JSONArray();
        dlrwgddy.put("title", "待录完工单(" + new SimpleDateFormat("yyyy-MM-dd").format(endXcsj) + "前)");
        dlrwgddy.put("type", "dlrwgdzdy");
        dlrwgddy.put("number", dlrwgddyResult.get("number"));
        dlrwgddy.put("result", dlrwgddyResult);
        dlrwgddy.put("path", "/gd/ywdwgd");
        two.put(dlrwgddy);

        // 待完善完工单定义
        JSONObject dwswgddy = new JSONObject();
//        JSONObject dwswgddyResult = getDzwgdData("dwswgdzdy", "dws");
        JSONObject dwswgddyResult = getWgdQuery("dwswgdzdy", "dws");
        // 解析时间
        Date end = dlrwgddyResult.getDate("endTime");
        dwswgddy.put("title", "待完善完工单("+ new SimpleDateFormat("yyyy-MM-dd").format(end) +"前)");
        dwswgddy.put("type", "dwswgdzdy");
        dwswgddy.put("number", dwswgddyResult.get("number"));
        dwswgddy.put("result", dwswgddyResult);
        dwswgddy.put("path", "/gd/ywdwgd");
        two.put(dwswgddy);

        // 用户已确认完工单
        JSONObject yhyqrwgd = new JSONObject();
//        JSONObject yhyqrwgdResult = handleWgd("dlwgdjt");
        JSONObject yhyqrwgdResult = queryWgd("yhyqr");
        JSONArray three = new JSONArray();
        yhyqrwgd.put("title", "用户待确认完工单");
        yhyqrwgd.put("type", "yhyqr");
        yhyqrwgd.put("number", yhyqrwgdResult.get("number"));
        yhyqrwgd.put("result", yhyqrwgdResult);
        yhyqrwgd.put("path", "/gd/ywdwgd");
        three.put(yhyqrwgd);

        // 未开发票
        JSONObject wkfp = new JSONObject();
//        JSONObject wkfpResult = handleWgd("wkfp");
        JSONObject wkfpResult = queryWgd("wkfp");
        wkfp.put("title", "未开发票");
        wkfp.put("type", "wkfp");
        wkfp.put("number", wkfpResult.get("number"));
        wkfp.put("result", wkfpResult);
        wkfp.put("path", "/gd/ywdwgd");
        three.put(wkfp);

        // 未回款
        JSONObject whk = new JSONObject();
//        JSONObject whkResult = handleWgd("whk");
        JSONObject whkResult = queryWgd("whk");
        JSONArray four = new JSONArray();
        whk.put("title", "未回款");
        whk.put("type", "whk");
        whk.put("number", whkResult.get("number"));
        whk.put("result", whkResult);
        whk.put("path", "/gd/ywdwgd");
        four.put(whk);

        // 未送回取回状态
        JSONObject shqhzt = new JSONObject();
        ZMailBo zMailBo = new ZMailBo();
        zMailBo.setShqhzt("0");
        zMailBo.setKdgs("sh,qh");
        List<ZMailVo> zMailVos = mailService.queryList(zMailBo);
        shqhzt.put("title", "未送回取回状态");
        shqhzt.put("type", "wshqh");
        shqhzt.put("number", zMailVos.size());
        shqhzt.put("result", zMailVos);
        shqhzt.put("path", "/gd/yjwp");
        four.put(shqhzt);

        // 下场通知用户
        JSONObject xctzyh = new JSONObject();
        JSONObject xctzyhResult = queryWgd("xctzyh");
        JSONArray five = new JSONArray();
        xctzyh.put("title", "下场通知用户");
        xctzyh.put("type", "xctzyh");
        xctzyh.put("result", xctzyhResult);
        xctzyh.put("number", xctzyhResult.get("number"));
        xctzyh.put("path", "/gd/ywdwgd");
        five.add(xctzyh);

        // 待申请电子证书
        JSONObject dsqdzzs = new JSONObject();
        JSONArray six = new JSONArray();
        JSONObject dsqdzzsResult = queryWgd("dsqdzzs");
        dsqdzzs.put("title", "待申请电子证书");
        dsqdzzs.put("type", "dsqdzzs");
        dsqdzzs.put("result", dsqdzzsResult);
        dsqdzzs.put("number", dsqdzzsResult.get("number"));
        dsqdzzs.put("path", "/gd/ywdwgd");
        six.add(dsqdzzs);

        // 电子档未发客户
        JSONObject dzdwfskh = new JSONObject();
        JSONObject dzdwfkhResult = queryWgd("dzdwfkh");
        dzdwfskh.put("title", "电子档未发客户");
        dzdwfskh.put("type", "dzdwfskh");
        dzdwfskh.put("result", dzdwfkhResult);
        dzdwfskh.put("number", dzdwfkhResult.get("number"));
        dzdwfskh.put("path", "/gd/ywdwgd");
        six.add(dzdwfskh);

        // 待打印纸质证书
        JSONObject ddyzzzs = new JSONObject();
        JSONArray seven = new JSONArray();
        JSONObject ddyzzzsResult = queryWgd("ddyzzzs");
        ddyzzzs.put("title", "待打印纸质证书");
        ddyzzzs.put("type", "ddyzzzs");
        ddyzzzs.put("result", ddyzzzsResult);
        ddyzzzs.put("number", ddyzzzsResult.get("number"));
        ddyzzzs.put("path", "/gd/ywdwgd");
        seven.add(ddyzzzs);

        // 邮寄状态
        JSONObject dzdwfkkh = new JSONObject();
        JSONObject dyjResult = queryWgd("dyj");
        dzdwfkkh.put("title", "待邮寄");
        dzdwfkkh.put("type", "dyj");
        dzdwfkkh.put("result", dyjResult);
        dzdwfkkh.put("number", dyjResult.get("number"));
        dzdwfkkh.put("path", "/gd/ywdwgd");
        seven.add(dzdwfkkh);

        // 添加完工单数据
        wgdList.add(one);
        wgdList.add(two);
        wgdList.add(three);
        wgdList.add(four);
        resultData.put("wgdList", wgdList);

        // 添加完工单数据
        qtList.add(five);
        qtList.add(six);
        qtList.add(seven);
        resultData.put("qtList", qtList);

        return resultData;
    }

    /**
     * 查询完工单数据
     * @param type
     * @return
     */
    private JSONObject queryWgd(String type) {
        // 1.定义返回数据
        JSONObject jsonObject = new JSONObject();
        ZYwdwgdBo zYwdwgdBo = new ZYwdwgdBo();
        if ("yhyqr".equals(type)) { // 用户已确认完工单 (0 否  1 是)
            zYwdwgdBo.setWgdfj("isNull");
        }else if ("wkfp".equals(type)) {  // 未开发票
            zYwdwgdBo.setSfkp("0");
        }else if ("whk".equals(type)) {  // 未回款
            zYwdwgdBo.setSfhk("3");
        }else if ("xctzyh".equals(type)) {  // 下场通知用户
            zYwdwgdBo.setXctzyh("0");
        }else if ("dsqdzzs".equals(type)) {  // 待申请电子证书
            zYwdwgdBo.setSfsqdzd("0");
        }else if ("dzdwfkh".equals(type)) {  // 电子单未发送给客户
            zYwdwgdBo.setSfgkhdzd("0");
        }else if ("ddyzzzs".equals(type)) {  // 待打印纸质证书
            zYwdwgdBo.setZzdy("0");
        }else if ("dyj".equals(type)) {  // 待邮寄
            zYwdwgdBo.setYyjkh("0");
        }
        List<ZYwdwgdVo> zYwdwgdVos = ywdwgdService.queryList(zYwdwgdBo);
        jsonObject.put("number", zYwdwgdVos.size());
        jsonObject.put("queryParams", zYwdwgdBo);
        jsonObject.put("resultData", zYwdwgdVos);
        return jsonObject;
    }

    /**
     * 查询完工单数据
     * @param type
     * @param zt
     * @return
     */
    private JSONObject getWgdQuery(String type, String zt) {
        // 1.定义返回数据
        JSONObject jsonObject = new JSONObject();
        ZYwdwgdBo zYwdwgdBo = new ZYwdwgdBo();
        // 判断当前的条件
        if ("dlrwgdjt".equals(type) || "dwswgdjt".equals(type)) {  // 待录入完工单今天
            // 创建一个 Calendar 实例
            Calendar calendar = Calendar.getInstance();
            // 获取当前月份的1号
            LocalDate firstDayOfYear = LocalDate.now().withMonth(1).withDayOfMonth(1);
            LocalDateTime startOfYear = firstDayOfYear.atStartOfDay();
            Date todayStart = Date.from(startOfYear.atZone(ZoneId.systemDefault()).toInstant());
            // 设置时间为今天23点59分59秒
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            // 获取今天的结束时间
            Date todayEnd = calendar.getTime();
            Map<String, Object> params = new HashMap<>();
            params.put("startXcrq", todayStart);
            params.put("endXcrq", todayEnd);
            zYwdwgdBo.setParams(params);
            jsonObject.put("endTime", todayEnd);
        }else {
            // 1.从系统变量获取当前的从前几天的数据
            String syDzwgdcx = configService.selectConfigByKey("sy_dzwgdcx");
            int dayNum = 3;
            if (StringUtils.isEmpty(syDzwgdcx)) {
                dayNum = Integer.parseInt(syDzwgdcx);
            }
            // 获取当前时间
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime threeDaysAgoEndTime = now.minusDays(dayNum).withHour(23).withMinute(59).withSecond(59);
            ZonedDateTime zonedDateTime = threeDaysAgoEndTime.atZone(ZoneId.systemDefault());
            Date endTime = Date.from(zonedDateTime.toInstant());

            // 获取当前月份的1号
            LocalDate firstDayOfYear = LocalDate.now().withMonth(1).withDayOfMonth(1);
            // 创建LocalDateTime并将时间设为0点
            LocalDateTime startOfYear = firstDayOfYear.atStartOfDay();
            // 转换为Date类型
            Date startTime = Date.from(startOfYear.atZone(ZoneId.systemDefault()).toInstant());
            Map<String, Object> params = new HashMap<>();
            params.put("startXcrq", startTime);
            params.put("endXcrq", endTime);
            zYwdwgdBo.setParams(params);
            jsonObject.put("endTime", endTime);
        }
        zYwdwgdBo.setWgdWszt(zt);
        List<ZYwdwgdVo> zYwdwgdVos = ywdwgdService.queryList(zYwdwgdBo);
        jsonObject.put("queryParams", zYwdwgdBo);
        jsonObject.put("resultData", zYwdwgdVos);
        jsonObject.put("number", zYwdwgdVos.size());
        return jsonObject;
    }

    // 完工单处理
    private JSONObject handleWgd(String type) {
        // 1.定义返回数据
        JSONObject jsonObject = new JSONObject();
        // 设置分页条件
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(1000);
        ZWgdBo zWgdBo = new ZWgdBo();
        if ("dlwgdjt".equals(type)) { // 用户已确认完工单
            zWgdBo.setYhyqr("1");
        }else if ("wkfp".equals(type)) {  // 未开发票
            zWgdBo.setSfykfp("N");
        }else if ("whk".equals(type)) {  // 未回款
            zWgdBo.setSfhk("3");
        }
        TableDataInfo<ZWgdVo> zWgdVoTableDataInfo = wgdService.queryPageList(zWgdBo, pageQuery);
        List<ZWgdVo> rows = zWgdVoTableDataInfo.getRows();
        jsonObject.put("number", rows.size());
        jsonObject.put("queryParams", zWgdBo);
        jsonObject.put("resultData", rows);

        return jsonObject;
    }

    // 获取当前的待完善完工单数量
    private JSONObject getDzwgdData(String type, String zt) {
        // 1.定义返回数据
        JSONObject jsonObject = new JSONObject();
        // 2.设置当前的查询条件
        ZYwdBo zYwdBo = new ZYwdBo();
        if ("dlrwgdjt".equals(type) || "dwswgdjt".equals(type)) {
            // 创建一个 Calendar 实例
            Calendar calendar = Calendar.getInstance();
            // 获取当前月份的1号
            LocalDate firstDayOfYear = LocalDate.now().withMonth(1).withDayOfMonth(1);
            LocalDateTime startOfYear = firstDayOfYear.atStartOfDay();
            Date todayStart = Date.from(startOfYear.atZone(ZoneId.systemDefault()).toInstant());
            // 设置时间为今天23点59分59秒
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
            // 获取今天的结束时间
            Date todayEnd = calendar.getTime();
            Map<String, Object> params = new HashMap<>();
            params.put("startTime", todayStart);
            params.put("endTime", todayEnd);
            params.put("wgdzt", zt);
            zYwdBo.setParams(params);
            jsonObject.put("endTime", todayEnd);
        }else {
            // 1.从系统变量获取当前的从前几天的数据
            String syDzwgdcx = configService.selectConfigByKey("sy_dzwgdcx");
            int dayNum = 3;
            if (StringUtils.isEmpty(syDzwgdcx)) {
                dayNum = Integer.parseInt(syDzwgdcx);
            }
            // 获取当前时间
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime threeDaysAgoEndTime = now.minusDays(dayNum).withHour(23).withMinute(59).withSecond(59);
            ZonedDateTime zonedDateTime = threeDaysAgoEndTime.atZone(ZoneId.systemDefault());
            Date endTime = Date.from(zonedDateTime.toInstant());

            // 获取当前月份的1号
            LocalDate firstDayOfYear = LocalDate.now().withMonth(1).withDayOfMonth(1);
            // 创建LocalDateTime并将时间设为0点
            LocalDateTime startOfYear = firstDayOfYear.atStartOfDay();
            // 转换为Date类型
            Date startTime = Date.from(startOfYear.atZone(ZoneId.systemDefault()).toInstant());
            Map<String, Object> params = new HashMap<>();
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            params.put("wgdzt", zt);
            zYwdBo.setParams(params);
            jsonObject.put("endTime", endTime);
        }

        // 设置分页条件
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(1000);
        TableDataInfo<ZYwdVo> ywd = ywdService.queryPageList(zYwdBo, pageQuery, "ywd");
        jsonObject.put("queryParams", zYwdBo);
        jsonObject.put("resultData", ywd.getRows());
        jsonObject.put("number", ywd.getRows().size());
        return jsonObject;
    }


}
